﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Rate;

namespace Backend.JYD.EFDAL.DAL
{
    public class WeightTempleteDAL : IDAL.Rate.IWeightTemplete
    {
        #region IWeightTemplete 成员
        public long AddWeightTemplete(WeightTemplete weight) {
            using (JydDataContext db = new JydDataContext())
            {
                //计算当前node的innercode值，先查询父节点的innercode
                weight.innercode = getNodeInnerCode(weight);
                weight.top_innercode = weight.innercode.Substring(0,3);
                weight.version = 1;
                db.WeightTempletes.Add(weight);
                db.SaveChanges();
                return weight.id;
            }
        }

        public bool UpdateWeightTemplete(WeightTemplete weight)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.WeightTempletes.Where(p => p.id == weight.id);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    temp.weight = weight.weight;
                    temp.description = weight.description;
                    //....
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public bool CancelWeightTemplete(Int64 weightId) {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.WeightTempletes.Where(p => p.id == weightId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    if (temp.Status <= 1)
                    {
                        temp.Status = -1;
                        return db.SaveChanges() > 0;
                    }
                }
            }
            return false;
        }

        public WeightTemplete GetWeightTempleteByWeightId(Int64 weightId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.WeightTempletes.Where(p => p.id == weightId);
                var temp = query.FirstOrDefault();
                return temp;
            }
        }

        public WeightTemplete GetWeightTempleteByModelId(Int64 modelId,int version)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.WeightTempletes.Where(p => p.model_id == modelId && p.version == version);
                var temp = query.FirstOrDefault();
                return temp;
            }
        }


        public IList<Model.Rate.WeightTemplete> loadChildWeights(Int64 modelId, Int64 version)
        {
            List<WeightTemplete> listWeight = new List<WeightTemplete>();
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.parent_id == modelId);
                List<RateModel> models = query.ToList();
                if(models != null){
                    for (int i = 0; i < models.Count;i++ )
                    {
                        var query2 = db.WeightTempletes.Where(p => p.model_id == models.ElementAt(i).id && p.version == version);
                        listWeight.Add(query2.FirstOrDefault());
                    }
                }
            }
            return listWeight;
        }

        public IList<WeightTemplete> loadAllWeights(long modelId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.WeightTempletes.Where(p => p.model_id == modelId).OrderBy(p => p.version);
                return query.ToList();
            }
        }

        public bool SaveTree(WeightTemplete weight) {
            //获取根节点
            string strInnerCode = weight.innercode;
            string upParentInnerCode = strInnerCode.Substring(0,3);
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.WeightTempletes.Where(p => p.top_innercode == upParentInnerCode).OrderBy(p => p.innercode);
                IList<WeightTemplete> listWeight = query.ToList();
                for (int i = 0; i < listWeight.Count; i++)
                {
                    WeightTemplete nowWeight = listWeight.ElementAt(i);
                    nowWeight.id = -1;
                    nowWeight.version = nowWeight.version + 1;
                    nowWeight.UpdateTime = DateTime.Now;
                    db.WeightTempletes.Add(nowWeight);
                    db.SaveChanges();
                }

            }
            return true;
        }

        private string getNodeInnerCode(WeightTemplete weight) {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.id == weight.model_id);
                RateModel model = query.FirstOrDefault();
                return model.innercode;
            }
        }
        #endregion
    }
}